/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package coms.ejb;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author David
 */
@MessageDriven(mappedName = "jms/EmailSenderMDB", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class EmailSenderMDB implements MessageListener {
    private static final String username="enterprisejava2012@gmail.com";
    private static final String password="teamse201ft";
    private static final String fromAddress="enterprisejava2012@gmail.com";
    
    public EmailSenderMDB() {

    }
    
    @Override
    public void onMessage(Message message) {
        //message contains recipient and message in Map type
        

            try {
               sendEmail(message);
            } catch (Exception ex) {
                Logger.getLogger(EmailSenderMDB.class.getName()).log(Level.SEVERE, null, ex);
            }
                
    }
    
    public void sendEmail(Message messageDetails) throws Exception{
       MapMessage msg = (MapMessage) messageDetails;
        try{
            System.out.println("Sending email");
            Properties props = new Properties();
            //for TLS
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.transport.protocol", "smtps");
               
            /**
            //for SSL
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.transport.protocol", "smtps");
            **/
            Session mailSession = Session.getInstance(props,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication(){
                            return new PasswordAuthentication(username, password);
                        }
                    
                    });
             
            javax.mail.Message message = new MimeMessage(mailSession);
            message.setFrom(new InternetAddress(fromAddress));
            message.setRecipients(javax.mail.Message.RecipientType.TO,InternetAddress.parse(msg.getString("recipient"),false));
            
            /*
            message.setSubject(msg.getString("Subject"));
            if (msg.getString("Subject")==null);
            */
            message.setSubject("COMS Notification Centre");
            message.setText(msg.getString("message"));
            
            Transport.send(message);
            
            /*
            Transport transport = mailSession.getTransport("smtps");
            transport.connect("smtp.gmail.com",465,"davidsimss@gmail.com", "david18101983");
            transport.send(message);
            transport.close();
            */
            
        }/*catch (Exception e){
            e.printStackTrace();
        }*/
        catch (MessagingException mex){
            System.out.println("MessagingException " + mex.getMessage());
        }
        
    }
    
}